
package linuxbox.bbs.servlet;

import javax.servlet.http.*;

import java.io.*;

import linuxbox.BoxStatic;
import linuxbox.bbs.logic.*;
import linuxbox.common.*;
import linuxbox.exception.*;

public class BbsProposeAction extends Action {
  protected static String successJSP = null;

  public void doAction(HttpSession aSession, BoxSession aBoxSession,
                       HttpServletRequest req, HttpServletResponse res) throws Exception {
    BbsManager bm = BbsManager.getInstance();

    //String sCatName = req.getParameter("CAT_NAME");

    String sCatID = req.getParameter("CAT_ID");
    int iCatID = 0;
    if (sCatID == null || sCatID.equals(""))
      throw new LinuxboxException(ExceptionHelper.BBS, "B000001");
    else
      iCatID = Integer.parseInt(sCatID);

    String sAuthor = req.getParameter("AUTHOR");
    if (sAuthor == null || sAuthor.equals(""))
      throw new LinuxboxException(ExceptionHelper.BBS, "B000005");

    String sSubject = req.getParameter("SUBJECT");
    if (sSubject == null || sSubject.equals(""))
      sSubject = "No subject";

    String sContent = req.getParameter("CONTENT");
    if (sContent == null)
      sContent = "";

    if (sContent.equals(""))
      sSubject = sSubject + "(No content)";

    String sMsgID = req.getParameter("MSG_ID");
    long lMsgID = Long.parseLong(sMsgID);

    String sTopicID = req.getParameter("TOPIC_ID");
    long lTopicID = Long.parseLong(sTopicID);

    String sIndLvl = req.getParameter("IND_LVL");
    int iIndLvl = Integer.parseInt(sIndLvl);

    java.sql.Date dtSubDate = new java.sql.Date(System.currentTimeMillis());
    java.sql.Time tmSubTime = new java.sql.Time(System.currentTimeMillis());

    String ip = req.getRemoteAddr();

    BbsMsg msg = null;
    if (lMsgID == -1) {
      //This is a new message
      msg = new BbsMsg(-1, -1, -1, 0);
    } else {
      //This is a reply message
      msg = new BbsMsg(-1, lTopicID, lMsgID, iIndLvl + 1);
    }

    msg.setSubDate(dtSubDate);
    msg.setSubTime(tmSubTime);
    msg.setSubject(sSubject);
    msg.setAuthor(sAuthor);
    msg.setCategory(iCatID);
    msg.setMsgBody(sContent);
    msg.setIP(ip);

    bm.addBbsMsg(msg);

    //sendFile(successJSP, req, res);

    res.setContentType("text/html; charset=gb2312");
    res.setHeader("Pragma", "no-cache");
    res.setHeader("Cache-Control", "no-cache");
    res.setDateHeader("Expires", 0);

    PrintWriter out = new PrintWriter(res.getOutputStream());
    out.print("<html><head><META HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL=");
    out.print(BoxStatic.servletDir);
    out.println("BbsContentAction?CAT_ID=" + iCatID + "\"></head></html>");

        /*PrintWriter out = new PrintWriter(res.getOutputStream());
  out.println("<html><head><META HTTP-EQUIV=\"Refresh\" CONTENT=\"0; "
      + "URL=/linuxbox/BoxServlet/BbsContentAction?CAT_ID="
   + iCatID + "\"></head></html>");*/

    out.flush();
    out.close();
  }

  public void setSuccessJSP(String str) {
    successJSP = str;
  }
}
